/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.Admin;
/*     */ import com.zimbra.cs.session.AdminSession;
/*     */ import com.zimbra.cs.session.Session;
/*     */ import com.zimbra.cs.session.Session.Type;
/*     */ import com.zimbra.cs.session.SessionCache;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.Collections;
/*     */ import java.util.Comparator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GetSessions
/*     */   extends AdminDocumentHandler
/*     */ {
/*     */   private static final String SESSION_KEY = "GetSessionsCachedResult";
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  47 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*     */     
/*  49 */     Server localServer = Provisioning.getInstance().getLocalServer();
/*  50 */     checkRight(zsc, context, localServer, Rights.Admin.R_getSessions);
/*     */     
/*  52 */     Element response = zsc.createElement(AdminConstants.GET_SESSIONS_RESPONSE);
/*     */     
/*  54 */     String typeStr = request.getAttribute("type");
/*     */     Session.Type type;
/*     */     try {
/*  57 */       type = Session.Type.valueOf(typeStr.toUpperCase());
/*     */     } catch (IllegalArgumentException e) {
/*  59 */       throw ServiceException.INVALID_REQUEST("invalid session type: " + typeStr, e);
/*     */     }
/*     */     
/*  62 */     SortBy sortBy = SortBy.fromString(request.getAttribute("sortBy", SortBy.nameAsc.name()));
/*  63 */     long offset = request.getAttributeLong("offset", 0L);
/*  64 */     long limit = request.getAttributeLong("limit", 2147483647L);
/*  65 */     boolean refresh = request.getAttributeBool("refresh", false);
/*     */     
/*  67 */     AdminSession adminSession = (AdminSession)getSession(zsc, Session.Type.ADMIN);
/*  68 */     CachedResult result = getResult(adminSession, type, refresh, sortBy);
/*  69 */     List<SessionInfo> sessions = result.sessions;
/*     */     
/*  71 */     int limitMax = (int)(offset + limit);
/*  72 */     for (int i = (int)offset; (i < limitMax) && (i < sessions.size()); i++) {
/*  73 */       ((SessionInfo)sessions.get(i)).encodeSession(response);
/*     */     }
/*     */     
/*  76 */     response.addAttribute("more", i < sessions.size());
/*  77 */     response.addAttribute("total", sessions.size());
/*     */     
/*  79 */     return response;
/*     */   }
/*     */   
/*     */   static enum SortBy {
/*  83 */     nameAsc,  nameDesc,  createdAsc,  createdDesc,  accessedAsc,  accessedDesc;
/*     */     
/*     */     private SortBy() {}
/*     */     
/*  87 */     public static SortBy fromString(String s) throws ServiceException { try { return valueOf(s);
/*     */       } catch (IllegalArgumentException e) {
/*  89 */         throw ServiceException.INVALID_REQUEST("invalid sortBy: " + s + ", valid values: " + Arrays.asList(values()), e); } } }
/*     */   
/*     */   static class SessionInfo { private String mAccountName;
/*     */     private String mAccountId;
/*     */     private String mSessionId;
/*     */     private long mCreated;
/*     */     private long mAccessed;
/*     */     
/*  97 */     SessionInfo(Session s, String name) { this.mAccountName = name;
/*  98 */       this.mAccountId = s.getAuthenticatedAccountId();
/*  99 */       this.mSessionId = s.getSessionId();
/* 100 */       this.mCreated = s.getCreationTime();
/* 101 */       this.mAccessed = s.getLastAccessTime();
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 110 */     public String getAccountName() { return this.mAccountName; }
/* 111 */     public String getAccountId() { return this.mAccountId; }
/* 112 */     public String getSessionId() { return this.mSessionId; }
/* 113 */     public long getCreated() { return this.mCreated; }
/* 114 */     public long getAccessed() { return this.mAccessed; }
/*     */     
/*     */     void encodeSession(Element parent) {
/* 117 */       Element sElt = parent.addElement("s");
/* 118 */       sElt.addAttribute("zid", this.mAccountId);
/* 119 */       sElt.addAttribute("name", this.mAccountName);
/* 120 */       sElt.addAttribute("sid", this.mSessionId);
/* 121 */       sElt.addAttribute("cd", this.mCreated);
/* 122 */       sElt.addAttribute("ld", this.mAccessed);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public CachedResult getResult(AdminSession adminSession, Session.Type type, boolean refresh, final SortBy sortBy)
/*     */   {
/* 134 */     List<Session> sessions = SessionCache.getActiveSessions(type);
/* 135 */     CachedResult result = (adminSession == null) || (refresh) ? null : (CachedResult)adminSession.getData("GetSessionsCachedResult");
/*     */     
/* 137 */     if ((result != null) && (result.type == type) && (result.sortBy == sortBy)) {
/* 138 */       return result;
/*     */     }
/* 140 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/* 142 */     result = new CachedResult();
/* 143 */     result.type = type;
/* 144 */     result.sortBy = sortBy;
/* 145 */     result.sessions = new ArrayList(sessions.size());
/* 146 */     for (Session s : sessions) {
/* 147 */       result.sessions.add(new SessionInfo(s, getName(prov, s.getAuthenticatedAccountId())));
/*     */     }
/*     */     
/*     */ 
/* 151 */     Comparator<SessionInfo> comparator = new Comparator() {
/*     */       public int compare(GetSessions.SessionInfo a, GetSessions.SessionInfo b) {
/*     */         long diff;
/* 154 */         switch (GetSessions.2.$SwitchMap$com$zimbra$cs$service$admin$GetSessions$SortBy[sortBy.ordinal()]) {
/* 155 */         case 1:  return a.getAccountName().compareToIgnoreCase(b.getAccountName());
/* 156 */         case 2:  return -a.getAccountName().compareToIgnoreCase(b.getAccountName());
/*     */         case 3: 
/* 158 */           diff = a.getAccessed() - b.getAccessed();
/* 159 */           return diff > 0L ? 1 : diff == 0L ? 0 : -1;
/*     */         case 4: 
/* 161 */           diff = a.getAccessed() - b.getAccessed();
/* 162 */           return diff > 0L ? -1 : diff == 0L ? 0 : 1;
/*     */         case 5: 
/* 164 */           diff = a.getAccessed() - b.getAccessed();
/* 165 */           return diff > 0L ? 1 : diff == 0L ? 0 : -1;
/*     */         case 6: 
/* 167 */           diff = a.getAccessed() - b.getAccessed();
/* 168 */           return diff > 0L ? -1 : diff == 0L ? 0 : 1;
/*     */         }
/* 170 */         return 0;
/*     */       }
/*     */       
/* 173 */     };
/* 174 */     Collections.sort(result.sessions, comparator);
/*     */     
/* 176 */     if (adminSession != null) {
/* 177 */       adminSession.setData("GetSessionsCachedResult", result);
/*     */     }
/* 179 */     return result;
/*     */   }
/*     */   
/*     */   private static String getName(Provisioning prov, String id) {
/*     */     try {
/* 184 */       Account acct = prov.get(Key.AccountBy.id, id);
/* 185 */       return acct == null ? id : acct.getName();
/*     */     } catch (ServiceException e) {}
/* 187 */     return id;
/*     */   }
/*     */   
/*     */ 
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 193 */     relatedRights.add(Rights.Admin.R_getSessions);
/*     */   }
/*     */   
/*     */   static class CachedResult
/*     */   {
/*     */     List<GetSessions.SessionInfo> sessions;
/*     */     Session.Type type;
/*     */     GetSessions.SortBy sortBy;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/GetSessions.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */